import {Actor, Action} from 'iflux2';
import {List} from 'immutable';
import {TradeType} from '../constant/action-type';


export default class TradeActor extends  Actor {
    defaultState() {
        return {
            addressList: [],
            tradeGoodsList: [],
            marketingOrder: {},
            ships: [],
            invoiceResponseList: [],
            payTypes: [],
            selfAddress: [],
            jifen: {},


            receiptId: '',
            shipId: '',
            shipFee: 0,
            payTypeId: '',
            selfId: '',
            invoice: {},
            couponId: undefined,
            integral: undefined,
            message: ''
        }
    }

    @Action(TradeType.SET_MESSAGE)
    setMessage(state, message) {
        return state.set('message', message);
    }


    @Action(TradeType.SET_COUPON_ID)
    setCouponId(state, couponId) {
        return state.set('couponId', couponId);
    }


    @Action(TradeType.SET_INTEGRAL)
    setIntegral(state, integral) {
        return state.set('integral', integral);
    }


    @Action(TradeType.SET_INVOICE)
    setInvoice(state, invoice) {
        return state.set('invoice', invoice);
    }



    @Action(TradeType.SET_JIFEN)
    setJifen(state, jifen) {
        return state.set('jifen', jifen);
    }


    @Action(TradeType.SET_SELF_ADDRESS_LIST)
    setSelfAddress(state, address) {
        return state.set('selfAddress', address);
    }


    @Action(TradeType.SET_SELF_ADDRESS_ID)
    setSelfAddressId(state, selfId) {
        return state.set('selfId', selfId);
    }


    @Action(TradeType.SET_ADDRESS_LIST)
    setList(state, addressList) {
        let defaultAddress = addressList.filter((address, index)=>(index == 0 || address.get('defaultReceipt') == 1));

        return state.set('addressList', addressList).set('receiptId', defaultAddress.getIn([defaultAddress.count() - 1, 'receiptId']));
    }


    @Action(TradeType.Add_SHIPPING_ADDRESS)
    addShippingAddresstoList(state, {shippingAddress, added}) {
        return state.update('addressList', (addressList)=>{
            if (added) {
                return addressList.concat(List.of(shippingAddress));
            } else {
                return addressList.map(address=>{
                    if (address.get('receiptId') == shippingAddress.get('receiptId')) {
                        return shippingAddress
                    }
                    return address;
                })
            }
        });
    }


    @Action(TradeType.SET_TRADE_GOODS_LIST)
    setGoodsList(state, tradeGoodsList) {
        return state.set('tradeGoodsList', tradeGoodsList);
    }


    @Action(TradeType.SET_MARKETING_ORDER)
    setMarketingOrder(state, marketingOrder) {
        return state.set('marketingOrder', marketingOrder);
    }


    @Action(TradeType.SET_SHIPS)
    setShips(state, ships) {
        return state.set('ships', ships).set('shipId', ships.getIn([0, 'id']));
    }


    @Action(TradeType.SET_SHIPPING_TYPE)
    setShippingType(state, id) {
        return state.set('shipId', id);
    }


    @Action(TradeType.SET_SHIP_FEE)
    setShipFee(state, shipFee) {
        return state.set('shipFee', shipFee);
    }


    @Action(TradeType.SET_INVOICE_LIST)
    setInvoiceResponseList(state, invoiceResponseList) {
        return state.set('invoiceResponseList', invoiceResponseList)
    }


    @Action(TradeType.SET_RECEIPT_ID)
    setReceiptId(state, receiptId) {
        return state.set('receiptId', receiptId);
    }


    @Action(TradeType.SET_PAY_TYPES)
    setPayTypes(state, payTypes) {
        return state.set('payTypes', payTypes).set('payTypeId', payTypes.getIn([0, 'payTypeId']));
    }


    @Action(TradeType.SET_PAY_TYPE_ID)
    setPayTypeId(state, payTypeId) {
        return state.set('payTypeId', payTypeId);
    }

}